require! <[ ./log.ls path chalk ]>
require! {
  \global-npm : npm
  \fs-extra : fs
}


NPM_REGISTRY = process.env.NPM_REGISTRY or \https://registry.npm.taobao.org

module.exports = (dir)~>
  if not await fs.exists path.join(dir,"package.json")
    return

  # require 会导致第二次没法加载到最新版号， 所以要读文件
  json = JSON.parse await fs.readFile path.join( dir, \package.json )
  {name,version} = json

  name-version = name
  if version
    name-version += " v#version"
  log.start(
    "开始安装 " + name-version
  )

  new Promise (resolve)!~>
    npm.load(
      {
        registry : NPM_REGISTRY
        loglevel : \http
        progress  : false
      }
      (err)!~>
        if err
          log.error err
        npm.commands.install(
          dir
          []
          !~>
            await fs.ensureDir path.join dir, \node_modules
            log.done "#name-version 安装成功\n"
            resolve!
        )
    )
